<template><div><h2 id="重置密码" tabindex="-1"><a class="header-anchor" href="#重置密码"><span>重置密码</span></a></h2>
<p>Laravel 5.5 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的密码重置功能" tabindex="-1"><a class="header-anchor" href="#laravel-的密码重置功能"><span>Laravel 的密码重置功能</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#resetting-database">数据库注意事项</a></li>
<li><a href="#resetting-routing">路由</a></li>
<li><a href="#resetting-views">视图</a></li>
<li><a href="#after-resetting-passwords">重置密码后</a></li>
<li><a href="#password-customization">自定义</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<blockquote>
<p>{tip} <strong>想要快速上手此功能？</strong> 只需在新的 Laravel 应用中运行 <code v-pre>php artisan make:auth</code> 命令，然后在浏览器中打开 <code v-pre>http://your-app.dev/register</code>，或者给应用分配任何一个 URL。这个命令会负责构建整个身份验证系统，包括重置密码！</p>
</blockquote>
<p>大部分 web 应用为用户提供了重置密码的功能。 Laravel 并不是强迫每个应用程序都要使用这个，仅仅只是想提供方法来发送密码提醒和执行密码重置。</p>
<blockquote>
<p>{note} 在使用 Laravel 的密码重置功能之前，你的用户模型必须使用 <code v-pre>Illuminate\Notifications\Notifiable</code> trait。</p>
</blockquote>
<h2 id="数据库注意事项" tabindex="-1"><a class="header-anchor" href="#数据库注意事项"><span>数据库注意事项</span></a></h2>
<p>开始之前，请确认你的 <code v-pre>App\User</code> 模型实现了 <code v-pre>Illuminate\Contracts\Auth\CanResetPassword</code> 契约。当然，Laravel 框架中包含的 <code v-pre>App\User</code> 模型已经实现了这个接口，并且使用 <code v-pre>Illuminate\Auth\Passwords\CanResetPassword</code> trait 来包含实现接口所需的方法。</p>
<h4 id="生成重置令牌的表迁移" tabindex="-1"><a class="header-anchor" href="#生成重置令牌的表迁移"><span>生成重置令牌的表迁移</span></a></h4>
<p>接下来，必须创建一个表来存储密码重置令牌。因为 Laravel 已经自带了用于生成这张表的迁移，就存放在 <code v-pre>database/migrations</code> 目录下。因此只需要运行数据库迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h2>
<p>Laravel 在 <code v-pre>Auth\ForgotPasswordController</code> 和 <code v-pre>Auth\ResetPasswordController</code> 这两个类中分别实现了通过邮件发送重置密码链接和重置密码的逻辑。执行密码重置所需的所有路由都可以使用 Artisan 命令 <code v-pre>make:auth</code> 生成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan make<span class="token punctuation">:</span>auth</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="视图" tabindex="-1"><a class="header-anchor" href="#视图"><span>视图</span></a></h2>
<p>当 Laravel 执行 <code v-pre>make:auth</code> 命令时，会在 <code v-pre>resources/views/auth/passwords</code> 目录下生成重置密码所需要的视图文件。你可以根据需要随意修改这些视图文件。</p>
<h2 id="重置密码后" tabindex="-1"><a class="header-anchor" href="#重置密码后"><span>重置密码后</span></a></h2>
<p>一旦你生成了用于重置用户密码的路由和视图，你就可以在浏览器中访问 <code v-pre>/password/reset</code> 这个路由来重置密码。框架中的 <code v-pre>ForgotPasswordController</code> 已经包含通过邮件发送重置密码链接的逻辑，而 <code v-pre>ResetPasswordController</code> 包含重置密码的逻辑。</p>
<p>重置密码后，用户就会自动登录并重定向到 <code v-pre>/home</code>。你可以修改 <code v-pre>ResetPasswordController</code> 中定义的 <code v-pre>redirectTo</code> 属性来自定义重置密码后重定向的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirectTo</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 默认情况下，重置密码的令牌一小时后会失效。你可以修改 <code v-pre>config/auth.php</code> 文件中的 <code v-pre>expire</code> 选项来修改这个过期时间。</p>
</blockquote>
<h2 id="自定义" tabindex="-1"><a class="header-anchor" href="#自定义"><span>自定义</span></a></h2>
<h4 id="自定义认证看守器" tabindex="-1"><a class="header-anchor" href="#自定义认证看守器"><span>自定义认证看守器</span></a></h4>
<p>在 <code v-pre>auth.php</code> 配置文件中，你可以配置多个「看守器」，可用于定义多个用户表的身份验证行为。可以通过在 <code v-pre>ResetPasswordController</code> 控制器上重写 <code v-pre>guard</code> 方法来指定你想使用的自定义的看守器。这个方法需要返回一个看守器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">guard</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">guard</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'guard-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义密码-broker" tabindex="-1"><a class="header-anchor" href="#自定义密码-broker"><span>自定义密码 Broker</span></a></h4>
<p>在 <code v-pre>auth.php</code> 配置文件中，你可以配置多个密码 「brokers」，用于多个用户表上的密码重置。你可以通过重写 <code v-pre>ForgotPasswordController</code> 和 <code v-pre>ResetPasswordController</code> 中的 <code v-pre>broker</code> 方法来选择你想使用的自定义 broker：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在密码重置期间获取使用代理。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name">PasswordBroker</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">broker</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">broker</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义用于重置的邮件" tabindex="-1"><a class="header-anchor" href="#自定义用于重置的邮件"><span>自定义用于重置的邮件</span></a></h4>
<p>你可以轻松地修改用于向用户发送密码重置链接的通知类。首先，重写 <code v-pre>User</code> 模型中的 <code v-pre>sendPasswordResetNotification</code> 方法。在这个方法中，你可以选择任何的通知类来发送通知。这个方法的第一个参数是用于重置密码的令牌 <code v-pre>$token</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 发送密码重置通知。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$token</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">sendPasswordResetNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ResetPasswordNotification</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/7083" target="_blank" rel="noopener noreferrer">@lockdown56</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/7083_1503468748.jpg" alt="7083_1503468748.jpg"></td>
<td>翻译</td>
<td>农闲出来敲代码，农忙回家种玉米</td>
</tr>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


